package com.mall507.fivepeaks.widget.xscrolltitlepager;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.mall507.fivepeaks.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 贺子航  2015/05/20 13:28
 * @description 滑动的带标题ViewPager
 */
public class XScrollTitlePager extends Fragment {
	  private List<TextView> mTitles=new ArrayList<TextView>();
	  private List<View> mViews=new ArrayList<View>();
	  private ViewPager mViewPager;
	  private LinearLayout mTitleNames;
	  private TextView mTitleScrollLeft;
	  private TextView mTitleScrollRight;
	  private int mWidth;//当前屏幕的宽
	  private String mTitleTextColorUp="#ffffff";
	  private String mTitleTextColorDown="#ffffff";

	  public XScrollTitlePager() {

	  }

	  /**
	   * @param names 标题名称s
	   * @param layouts Item布局文件s
	   * */
	  public static XScrollTitlePager newInstance(String[] names,int[] layouts) {
		XScrollTitlePager model = new XScrollTitlePager();
		Bundle bu=new Bundle();
		bu.putStringArray("names", names);
		bu.putIntArray("layouts", layouts);
		model.setArguments(bu);
	    return model;
	  }

	  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
	    super.onCreateView(inflater, container, savedInstanceState);
	    LinearLayout view = (LinearLayout) inflater.inflate(R.layout.view_xscrolltitlepager, null);
	    mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
	    mTitleNames = (LinearLayout) view.findViewById(R.id.title_names);
	    mTitleScrollLeft = (TextView) view.findViewById(R.id.title_scroll_left);
	    mTitleScrollRight = (TextView) view.findViewById(R.id.title_scroll_right);
		for(int i=0;i<getArguments().getIntArray("layouts").length;i++){
			mViews.add(inflater.inflate(getArguments().getIntArray("layouts")[i], null));
		}
	    initLayout();
	    return view;
	  }

	  /**
	   * @author 贺子航  2015/04/09 16:37
	   * @description 初始ViewPager
	   */
	  public void initLayout() {
		initTitle();
	    // 设置填充ViewPager页面的适配器
		mViewPager.setAdapter(new MyAdapter());
	    // 设置一个监听器，当ViewPager中的页面改变时调用
		mViewPager.setOnPageChangeListener(new MyPageChangeListener());
	  }
	  
	  /**
	   * @author 贺子航  2015/04/09 16:37
	   * @description 初始title
	   */
	  public void initTitle() {
		  //title名称
		  mTitles.clear();
		  mTitleNames.removeAllViews();
		  for(int i=0;i<getArguments().getIntArray("layouts").length;i++){
		    TextView title=new TextView(getActivity());
		    title.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,1));
		    title.setGravity(Gravity.CENTER);
		    if(i<getArguments().getStringArray("names").length){
		    	title.setText(getArguments().getStringArray("names")[i]);
		    }else{
		    	title.setText("未命名");
		    }
		    if(i==0){
		    	title.setTextColor(Color.parseColor(mTitleTextColorDown));
		    }else{
		    	title.setTextColor(Color.parseColor(mTitleTextColorUp));
		    }
		    mTitles.add(title);
		    mTitleNames.addView(title);
		  }
		  //title滚动条
		  DisplayMetrics dm = new DisplayMetrics();
		  getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		  mWidth=dm.widthPixels;
		  mTitleScrollRight.setLayoutParams(new LinearLayout.LayoutParams(mWidth/mViews.size(),LayoutParams.MATCH_PARENT));
		  mTitleScrollRight.setBackgroundColor(Color.parseColor(mTitleTextColorDown));
	  }
	  
	  

	  /**
	   * @author 贺子航  2015/04/09 16:37
	   * @description ViewPager | 适配器
	   */
	  private class MyAdapter extends PagerAdapter {

	    @Override
	    public int getCount() {
	      return mViews.size();
	    }

	    /**
	     * 从当前container中删除指定位置（position）的View
	     * **/
	    @Override public void destroyItem(ViewGroup container, int position,Object object) {
	      container.removeView((View)object);
	    }

	    /**
	     * 做了两件事，第一：将当前视图添加到container中，第二：返回当前View
	     * **/
	    @Override public Object instantiateItem(ViewGroup container, int position) {
	      container.addView(mViews.get(position));
	      return mViews.get(position);
	    }

	    /**
	     * 该函数用来判断instantiateItem(ViewGroup, int)函数所返回来的Key与一个页面视图是否是代表的同一个视图
	     * (即它俩是否是对应的，对应的表示同一个View)
	     * 返回值：如果对应的是同一个View，返回True，否则返回False。
	     * **/
	    @Override public boolean isViewFromObject(View view, Object key) {
	      return view == key;
	    }
	  }

	  
	  /**
	   * @author 贺子航  2014-04-07  15:50
	   * @description ViewPager | 滑动监听
	   */
	  private class MyPageChangeListener implements ViewPager.OnPageChangeListener {
		  
	    /**
	     * 此方法是页面跳转完后得到调用
	     * arg0是你当前选中的页面的Position(位置编号0开始)
	     * **/
	    public void onPageSelected(int position) {
	    	for(int i=0;i<mTitles.size();i++){
	    		if(i==position){
	    		  mTitles.get(i).setTextColor(Color.parseColor(mTitleTextColorDown));
	    		}else{
	    		  mTitles.get(i).setTextColor(Color.parseColor(mTitleTextColorUp));
	    		}
	    	}
	    }

	    /**
	     * 在状态改变的时候调用
	     * 其中arg0这个参数有三种状态(0，1，2):
	     * arg0 == 0 :的时候表示什么都没做
	     * arg0 == 1 :的时候表示正在滑动
	     * arg0 == 2 :的时候表示滑动完毕了
	     * **/
	    public void onPageScrollStateChanged(int arg0) {
	    }

	    /**
	     * 在滑动的时候会调用此方法,在滑动被停止之前，此方法回一直得到调用
	     * 其中三个参数的含义分别为：
	     * arg0 :当前页面，及你点击滑动的页面
	     * arg1 :当前页面偏移的百分比
	     * arg2 :当前页面偏移的像素位置
	     * **/
	    public void onPageScrolled(int arg0, float arg1, int arg2) {
	    	//当前页,滚动栏距最左边的基础距离
	    	int defaultWidth=0;
	    	if(arg0==0){
	    		defaultWidth=0;
	    	}else{
	    		//(views.size()-1):处理除不尽的情况右边有一些小空隙
	    		defaultWidth=(mWidth/mViews.size()*arg0)+(mViews.size()-1);
	    	}
	    	if(arg2!=0){
	    		mTitleScrollLeft.setLayoutParams(new LinearLayout.LayoutParams(defaultWidth+arg2/mViews.size(),LayoutParams.MATCH_PARENT));
		    }
	    }
	  }
}
